<template>
  <el-container class="home">
    <el-header>
      <div class="logo">
        <img src="../assets/logo.png" alt="" />
        易购后台管理系统
      </div>
      <el-button type="primary" @click="loginout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggleIcon" @click="toggleCollapse">
          <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"></i>
        </div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#408eff" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <el-submenu :index="menu.id + ''" v-for="menu in menuList" :key="menu.id">
            <template slot="title">
              <i :class="menuIcon[menu.id]"></i>
              <span>{{ menu.authname }}</span>
            </template>
            <el-menu-item :index="'/' + menuItem.path" v-for="menuItem in menu.children" :key="menuItem.id" @click="addActivePath('/' +menuItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ menuItem.authname }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      menuList: [],
      menuIcon: {
        1: 'el-icon-user',
        2: 'el-icon-lock',
        3: 'el-icon-goods',
        4: 'el-icon-shopping-cart-1',
        5: 'el-icon-files'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this._getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginout() {
      window.sessionStorage.removeItem('token')
      window.sessionStorage.removeItem('activePath')
      this.$router.push('/login')
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    async _getMenuList() {
      const { data: res } = await this.$http.get('/getMenus')
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message({
          showClose: true,
          message: res.meta.msg,
          type: 'error'
        })
      }
      this.menuList = res.data
    },
    addActivePath(newPath) {
      window.sessionStorage.setItem('activePath', newPath)
      this.activePath = newPath
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.home {
  width: 100%;
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  align-items: center;
  .logo {
    display: flex;
    flex: 1;
    color: #fff;
    font-size: 20px;
    align-items: center;
    img {
      width: 25px;
      height: 25px;
      margin-right: 10px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right-width: 0;
  }
  .toggleIcon {
    width: 100%;
    text-align: center;
    background-color: #000;
    padding: 5px 0;
    cursor: pointer;
    i {
      font-size: 20px;
      width: 30px;
      color: #909399;
    }
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>
